package com.unionpay.upyzt.spring.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.InvalidRequestException;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.BalanceAcct;
import com.unionpay.upyzt.model.Payment;
import com.unionpay.upyzt.model.SettleAcct;
import com.unionpay.upyzt.model.Withdrawal;
import com.unionpay.upyzt.resp.*;
import com.unionpay.upyzt.spring.example.test.entity.Dto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/addAcct")
    public String getStr(@RequestBody Dto dto){
        String s = null;
        String cusId="1111598881267929095";
        Map<String, Object> params = new HashMap<>();
        params.put("out_request_no", UUID.randomUUID().toString());
        params.put("cus_id", cusId);
        params.put("bank_acct_type", "1");
        try {
            params.put("bank_acct_no",Upyzt.encryptField("6215581304000900572"));
            params.put("sms_code","201808");
        } catch (InvalidRequestException e) {
            throw new RuntimeException(e);
        }
        try {
            SettleAcctResp settleAcctResp = SettleAcct.create(params);
            System.out.println(settleAcctResp.toString());
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }

        return s;
    }


    @PostMapping("/getMoney")
    public String getMoney(@RequestBody Dto dto){
        String s = null;
      /*  String cusId="1111576011121283686";*/
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", UUID.randomUUID().toString());
       /* params.put("cus_id", cusId);*/
        params.put("bank_acct_type", "1");
        params.put("balance_acct_id", "2011944128937936491");
        params.put("business_type","1");
        params.put("amount",1);
        try {
            params.put("password",Upyzt.encryptField("123456"));
        } catch (InvalidRequestException e) {
            throw new RuntimeException(e);
        }
        try {
            params.put("bank_acct_no",Upyzt.encryptField("6217001630070663130"));
          /*  params.put("sms_code","490106");*/
        } catch (InvalidRequestException e) {
            throw new RuntimeException(e);
        }
        try {
            WithdrawalResp withdrawalResp = Withdrawal.create(params);
            System.out.println(withdrawalResp.toString());
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }

        return s;
    }

    @PostMapping("/changeAmount")
    public String changeAmount(@RequestBody Dto dto){
        String s = null;
        /*  String cusId="1111576011121283686";*/
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", UUID.randomUUID().toString());
        /* params.put("cus_id", cusId);*/
        params.put("recv_balance_acct_id", "2011944128937936491");
        params.put("pay_balance_acct_id", "2011576011229661793");
        params.put("amount",1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("order_amount",1);
        map.put("order_no",UUID.randomUUID().toString());
        map.put("product_name","转账");
        map.put("product_count",1);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(map);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("product_infos",objects);
        params.put("extra",map1);
        try {
            params.put("password",Upyzt.encryptField("169223"));
        } catch (InvalidRequestException e) {
            throw new RuntimeException(e);
        }
        try {
            params.put("bank_acct_no",Upyzt.encryptField("623061551011380596"));
            /*  params.put("sms_code","490106");*/
        } catch (InvalidRequestException e) {
            throw new RuntimeException(e);
        }
        try {
            PaymentResp paymentResp = Payment.create(params);
            System.out.println(paymentResp.toString());
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }

        return s;
    }
    /*{
  "verify_status": "succeeded",
  "bank_acct_type": "1",
  "bank_code": "313331000014",
  "name": "汪江佩",
  "bank_address_code": null,
  "bank_branch_code": "313331000014",
  "bank_acct_no": "623061551011380596",
  "mobile_number": "13345931406",
  "acct_validation": null,
  "settle_acct_id": "2111933626549041708",
  "out_request_no": "ddb20b47-d7d2-40be-b63a-379e2290153f",
  "acct_validation_finished_at": "2025-03-08T13:18:11.566",
  "acct_validation_failure_msg": null,
  "metadata": null,
  "extra": null,
  "request_id": "96ef624eb91a4e388fc3b85ad06610ad"
}
*/

    @PostMapping("/selectAcct")
    public SettleAcctListResp getStr1(@RequestBody Dto dto){
        HashMap<String, Object> map = new HashMap<>();
        map.put("cus_id","1111601359145750584");
        SettleAcctListResp settleAcctListResp = null;
        try {
             settleAcctListResp = SettleAcct.retrieveByUserId(map);
            System.out.println(settleAcctListResp.toString());
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }

        return settleAcctListResp;
    }
    @PostMapping("/delAcct")
    public String delAcct(@RequestBody Dto dto){
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("cus_id","1111598881267929095");
            map.put("bank_acct_no",Upyzt.encryptField("6217001630070663130"));
            SettleAcctDeleteResp settleAcctDeleteResp = SettleAcct.removeByParams(map,null);
            System.out.println(settleAcctDeleteResp.toString());
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }

        return null;
    }


    @PostMapping("/delAcct2")
    public String getStr3(@RequestBody Dto dto){
        BalanceAcctResp balanceAcctResp = null;
        try {
            balanceAcctResp = BalanceAcct.retrieveById("2011576011229661793");
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }
        System.out.println(balanceAcctResp);

        return null;
    }


    @PostMapping("/createBalance")
    public String createBalance(@RequestBody Dto dto){
        HashMap<String, Object> map = new HashMap<>();
        map.put("out_request_no", UUID.randomUUID().toString());
        map.put("cus_id","1111598881267929095");
        BalanceAcctStoreResp balanceAcctStoreResp = null;
        try {
            map.put("password",Upyzt.encryptField("123456"));
            balanceAcctStoreResp = BalanceAcct.create(map);
        } catch (UpyztException e) {
            throw new RuntimeException(e);
        }
        System.out.println(balanceAcctStoreResp);

        return null;
    }
}
